package com.leemon.mall.sys.controller;


import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.leemon.mall.common.util.CacheManagerUtil;
import com.leemon.mall.sys.model.SysConfig;
import com.leemon.mall.sys.service.SysConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import org.springframework.stereotype.Controller;

/**
 * <p>
 * 系统配置信息表 前端控制器
 * </p>
 *
 * @author leemon
 * @since 2019-09-04
 */
@RestController
@Api(tags = "后台-参数配置接口",description = "系统配置参数")
@RequestMapping("/sys/config")
public class SysConfigController {

    @Autowired
    private SysConfigService sysConfigService;

    @GetMapping("/page")
    @ApiOperation("所有配置列表")
    public ResponseEntity<IPage<SysConfig>> page(String paramKey, Page<SysConfig> page){
        IPage<SysConfig> sysConfigs = sysConfigService.page(page, new LambdaQueryWrapper<SysConfig>().like(StrUtil.isNotBlank(paramKey),SysConfig::getParamKey,paramKey));
        return ResponseEntity.ok(sysConfigs);
    }

    @GetMapping("/info/{id}")
    @ApiOperation("配置信息")
    public ResponseEntity<SysConfig> info(@PathVariable("id") Long id){
        SysConfig config = sysConfigService.getById(id);
        return ResponseEntity.ok(config);
    }

    @ApiOperation("保存配置")
    @PostMapping
    public ResponseEntity<Void> save(@RequestBody SysConfig config){
        sysConfigService.save(config);
        return ResponseEntity.ok().build();
    }

    /**
     * 修改配置
     */
    @ApiOperation("修改配置")
    @PutMapping
    public ResponseEntity<Void> update(@RequestBody SysConfig config){
        sysConfigService.updateById(config);
        return ResponseEntity.ok().build();
    }

    /**
     * 删除配置
     */
    @ApiOperation("删除配置")
    @DeleteMapping
    public ResponseEntity<Void> delete(@RequestBody Long[] configIds){
        sysConfigService.deleteBatch(configIds);
        return ResponseEntity.ok().build();
    }

}

